package db;

import java.awt.Color;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.SwingConstants;

import java.awt.Dimension;

import javax.swing.JSeparator;
import javax.swing.ImageIcon;


import product.SaleUpdate;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class SaleList extends JDesktopPane {

	String str;
	Vector<String> col = new Vector<String>();
	Connection con;
	private JTextField findStr;
	private JButton button;
	private JScrollPane scrollPane;
	DefaultTableModel model;
	private JTable table;
	ButtonGroup bg = new ButtonGroup();
	private JRadioButton btn1;
	private JRadioButton btn2;
	private JPanel panel;
	DefaultTableModel mod;
	Vector<String> vec = new Vector<String>();
	Main m;
	private JLabel label;
	private JSeparator separator;
	private JLabel label_1;
	ResultSetMetaData rsm;
	private JTextField code;
	private JLabel label_2;
	private JLabel label_3;
	private JTextField codename;
	private JTextField price;
	private JLabel label_4;
	private JLabel label_5;
	private JTextField ea;
	private JButton button_1;
	private JButton button_2;

	public SaleList(Connection con, Main m) {
		this.m = m;
		this.con = con;

		setBackground(Color.BLACK);
		setLayout(null);
		add(getFindStr());
		add(getButton());
		add(getScrollPane());
		add(getPanel());
		bg.add(btn1);
		bg.add(btn2);
		add(getLabel());
		add(getSeparator());
		add(getLabel_1());
		btn2.setSelected(true);
		table.setRowHeight(25);
		add(getCode());
		add(getLabel_2());
		add(getLabel_3());
		add(getCodename());
		add(getPrice());
		add(getLabel_4());
		add(getLabel_5());
		add(getEa());
		add(getButton_1());
		add(getButton_2());
	}

	private boolean search() {
		try {
			String find = findStr.getText();
			// 가져올 자료 요청
			String sql = "select sa_no,s.sa_pro_code,c.pro_nm,s.sa_ea,s.sa_price,s.sa_recall,s.sa_reg_date,s.sa_reg_mem,s.sa_mod_date,s.sa_mod_mem "
					+ "from sale s inner join code c on c.pro_code = s.sa_pro_code where sa_pro_code like ? or c.pro_nm like ? order by s.sa_no asc";
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, "%" + find + "%");
			pstmt.setString(2, "%" + find + "%");
			ResultSet rs = pstmt.executeQuery();
			rsm = rs.getMetaData();
			Vector col = new Vector<>();
			col.add("판매번호");
			col.add("제품코드");
			col.add("제품이름");
			col.add("판매수량");
			col.add("판매가격");
			col.add("반품여부");
			col.add("판매일");
			col.add("판매사원");
			col.add("수정일");
			col.add("수정자");

			model = new DefaultTableModel(col, 0) {
				public boolean isCellEditable(int rowIndex, int mColIndex) {
					return false;
				}
			};
			table.setModel(model);
			while (rs.next()) {
				Vector d = new Vector<>();
				for (int i = 1; i <= rsm.getColumnCount(); i++) {
					d.add(rs.getString(i));
				}
				model.addRow(d);
			}
			rs.close();
			pstmt.close();
			if (model.getRowCount() == 0)
				JOptionPane.showMessageDialog(null, "해당 정보는 존재하지 않습니다.");
			return true;
		} catch (Exception e) {
			System.out.println("예외사항:" + e.getMessage());
			return false;
		}
	}

	private boolean search2() {
		try {

			String sql = "select c.pro_code,c.pro_nm,sum(s.sa_ea),sum(s.sa_price) from sale s inner join"
					+ " code c on c.pro_code = s.sa_pro_code where s.sa_pro_code like '%%' group by c.pro_code, c.pro_nm";
			PreparedStatement pstmt = con.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			rsm = rs.getMetaData();
			Vector col = new Vector<>();
			col.add("제품코드");
			col.add("제품이름");
			col.add("판매수량");
			col.add("판매가격");

			model = new DefaultTableModel(col, 0) {
				public boolean isCellEditable(int rowIndex, int mColIndex) {
					return false;
				}
			};
			table.setModel(model);
			while (rs.next()) {
				Vector d = new Vector<>();
				for (int i = 1; i <= rsm.getColumnCount(); i++) {
					d.add(rs.getString(i));
				}

				model.addRow(d);

			}
			rs.close();
			pstmt.close();
			if (model.getRowCount() == 0)
				JOptionPane.showMessageDialog(null, "해당 정보는 존재하지 않습니다.");
			return true;
		} catch (Exception e) {
			System.out.println("예외사항:" + e.getMessage());
			return false;
		}
	}

	private JTextField getFindStr() {
		if (findStr == null) {
			findStr = new JTextField();
			findStr.setColumns(10);
			findStr.setBounds(12, 114, 541, 28);
		}
		return findStr;
	}

	private JButton getButton() {
		if (button == null) {
			button = new JButton("조 회");
			button.setBackground(Color.BLACK);
			button.setForeground(Color.WHITE);
			button.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 30));
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (btn2.isSelected())
						search();
					else if (btn1.isSelected()) {
						search2();
					}
				}
			});
			button.setBounds(561, 113, 93, 30);
		}
		return button;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 151, 642, 285);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}

	private JTable getTable() {
		if (table == null) {
			table = new JTable(model);
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					if (btn2.isSelected())
						MouseC();
				}
			});
		}
		return table;
	}

	public void MouseC() {
		int index = table.getSelectedRow();

		// Vector<String> vec = new Vector<String>();
		code.setText("" + table.getValueAt(index, 1));
		codename.setText("" + table.getValueAt(index, 2));
		ea.setText("" + table.getValueAt(index, 3));
		price.setText(table.getValueAt(index, 4) + "");

		// UpdateFrame up = new db.UpdateFrame();

		m.setBounds(100, 100, 1150, 533);
		// this.add(up);
		// up.setVisible(true);

	}

	private JRadioButton getBtn1() {
		if (btn1 == null) {
			btn1 = new JRadioButton("제품별 판매 총액");
			btn1.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 20));
			btn1.setForeground(Color.WHITE);
			btn1.setBackground(Color.BLACK);
		}
		return btn1;
	}

	private JRadioButton getBtn2() {
		if (btn2 == null) {
			btn2 = new JRadioButton("모든 판매 기록");
			btn2.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 20));
			btn2.setForeground(Color.WHITE);
			btn2.setBackground(Color.BLACK);
		}
		return btn2;
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBackground(Color.BLACK);
			panel.setBounds(97, 66, 440, 42);

			panel.add(getBtn1());
			panel.add(getBtn2());
		}
		return panel;
	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("판매 조회");
			label.setPreferredSize(new Dimension(700, 25));
			label.setHorizontalAlignment(SwingConstants.CENTER);
			label.setForeground(Color.WHITE);
			label.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 50));
			label.setBounds(0, 10, 679, 31);
		}
		return label;
	}

	private JSeparator getSeparator() {
		if (separator == null) {
			separator = new JSeparator();
			separator.setPreferredSize(new Dimension(380, 5));
			separator.setBounds(22, 51, 917, 5);
		}
		return separator;
	}

	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("중앙HTA 할인매장 - 5조");
			label_1.setForeground(Color.WHITE);
			label_1.setFont(new Font("나눔손글씨 펜", Font.BOLD, 25));
			label_1.setBounds(474, 455, 218, 28);
		}
		return label_1;
	}

	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setText((String) null);
			code.setPreferredSize(new Dimension(6, 25));
			code.setHorizontalAlignment(SwingConstants.CENTER);
			code.setFont(new Font("맑은 고딕", Font.BOLD, 16));
			code.setEditable(false);
			code.setColumns(12);
			code.setBounds(776, 114, 163, 30);
		}
		return code;
	}

	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("제품 코드");
			label_2.setHorizontalAlignment(SwingConstants.CENTER);
			label_2.setForeground(Color.WHITE);
			label_2.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 23));
			label_2.setBounds(690, 114, 70, 30);
		}
		return label_2;
	}

	private JLabel getLabel_3() {
		if (label_3 == null) {
			label_3 = new JLabel("제품명");
			label_3.setHorizontalAlignment(SwingConstants.CENTER);
			label_3.setForeground(Color.WHITE);
			label_3.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 23));
			label_3.setBounds(690, 155, 70, 30);
		}
		return label_3;
	}

	private JTextField getCodename() {
		if (codename == null) {
			codename = new JTextField();
			codename.setText((String) null);
			codename.setHorizontalAlignment(SwingConstants.CENTER);
			codename.setFont(new Font("맑은 고딕", Font.BOLD, 16));
			codename.setEditable(false);
			codename.setColumns(12);
			codename.setBounds(776, 155, 163, 30);
		}
		return codename;
	}

	private JTextField getPrice() {
		if (price == null) {
			price = new JTextField();
			price.setText((String) null);
			price.setHorizontalAlignment(SwingConstants.CENTER);
			price.setFont(new Font("맑은 고딕", Font.BOLD, 16));
			price.setEditable(false);
			price.setColumns(12);
			price.setBounds(776, 196, 163, 30);
		}
		return price;
	}

	private JLabel getLabel_4() {
		if (label_4 == null) {
			label_4 = new JLabel("판매가");
			label_4.setHorizontalAlignment(SwingConstants.CENTER);
			label_4.setForeground(Color.WHITE);
			label_4.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 23));
			label_4.setBounds(690, 196, 70, 26);
		}
		return label_4;
	}

	private JLabel getLabel_5() {
		if (label_5 == null) {
			label_5 = new JLabel("판매 수량");
			label_5.setHorizontalAlignment(SwingConstants.CENTER);
			label_5.setForeground(Color.WHITE);
			label_5.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 23));
			label_5.setBounds(690, 236, 74, 30);
		}
		return label_5;
	}

	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setText((String) null);
			ea.setHorizontalAlignment(SwingConstants.CENTER);
			ea.setFont(new Font("맑은 고딕", Font.BOLD, 16));
			ea.setColumns(12);
			ea.setBounds(776, 236, 163, 30);
		}
		return ea;
	}

	private JButton getButton_1() {
		if (button_1 == null) {
			button_1 = new JButton("반 품");
			button_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {

				}
			});
			button_1.setForeground(Color.WHITE);
			button_1.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 30));
			button_1.setBackground(Color.BLACK);
			button_1.setBounds(842, 276, 97, 23);
		}
		return button_1;
	}

	public void modi() {
		try {

			String sql = "update sale set sa_ea=?, sa_price=?*(select pro_price*1.2 from code where pro_code=?) where sa_pro_code=?";
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, ea.getText());
			pstmt.setString(2, ea.getText());
			pstmt.setString(3, code.getText());
			pstmt.setString(4, code.getText());
			ResultSet rs = pstmt.executeQuery();
			
			rs.close();
			pstmt.close();
		} catch (SQLException s) {
			s.toString();
		}
	}

	private JButton getButton_2() {
		if (button_2 == null) {
			button_2 = new JButton("수 정");
			button_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					modi();
				}
			});
			button_2.setForeground(Color.WHITE);
			button_2.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 30));
			button_2.setBackground(Color.BLACK);
			button_2.setBounds(690, 276, 97, 23);
		}
		return button_2;
	}

}